"""
s = 1
print(isinstance(s, object))

class Person:
    pass 
p = Person()
print(isinstance(p, object))

d = dict()
print(isinstance(d, object))

tem_list = ['a','b','c','d']
tem_list2 = tem_list[::-1]
tem_list3 = tem_list[::-2]
tem_list4 = tem_list[:-1]
print(tem_list2)
print(tem_list3)
print(tem_list4) # [a,b,c]

list1 = [1,2,3]
list2 = [1,2,3]
print(list1 is list2) # False
print(list1 == list2) # True

list3 = [1,2,3,{'a':'a'}]
list4 = [1,2,3,{'a': 'a'}]
print(list3 is list4) # False
print(list3 == list4) # True
s = {'a'}
print(type(s)) # <class 'set'>

a = 100
b = 100
s1 = "Hello"
s2 = "Hello"

print(a is b)
print(s1 is s2)
"""
# Immutable: 不可变对象, 这些类型的对象在创建后，其内部数据不能被修改。
# 如果对它们进行“修改”操作，实际上是创建了一个新的对象，原对象保持不变。
# 多个变量可以安全地共享同一个不可变对象
# 可以作为字典的键
"""
int
float
str
tuple
bytes
frozenset
"""

"""
t = (1,2,3)
t2 = (1,2,3)
print(t is t2) # True
print(t == t2) # True
t = (1,2,3)
d = {}
d[t] = 4
print(d)
print(d[t])
"""
# Mutable: 可变对象
# list、set、dict、bytearray
# 可以在原地址修改
# 不能作为字典的键


# ! Python 只在函数定义时计算一次默认参数，
# ! 这意味着如果默认参数是可变对象，后续对它的修改会影响到后续的函数调用。

"""
def test(l = []):
    l.append(1)
    print(l)
test() # [1]
test() # [1,1]
"""

"""
list1 = ['a','b','c','d']
list2 = list1[::]
print(list2)
"""

"""
n1 = None
n2 = None
l1 = [1,2,3]
print(n1 is l1)
print(n1 is n2)
print(n1 == n2)
"""

# 原因: is操作符直接比较对象的内存地址，
# 而==操作符需要调用对象的__eq__()方法，这涉及到额外的方法调用开销
"""
value = None
if value is None:  #  正确
    print("value 是 None")

if value == None:  #  不推荐
    print("value 是 None")

"""

# 危险：共享可变对象
def create_matrix(rows, cols):
    return [[0] * cols] * rows  # 所有行共享同一个列表

# 安全：创建独立对象
def create_matrix_safe(rows, cols):
    return [[0] * cols for _ in range(rows)]

matrix = create_matrix(3, 3)
matrix[0][0] = 1
print(matrix)
matrix_safe = create_matrix_safe(3, 3)
matrix_safe[0][0] = 1 
print(matrix_safe)


"""
list1 = [1,2,3]
list2 = list1 * 3
print(list2) # [1, 2, 3, 1, 2, 3, 1, 2, 3]
list3 = [0] * 3
print(list3) # [0, 0, 0]
lsit4 = [[0] * 3] # 猜测[[0],[0],[0]]  正确: [[0, 0, 0]]
print(lsit4)
"""

"""
l = [[0, 0, 0]] * 3 # [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(l)
print(l[0] is l[1])
l2 = [{'a':'a'},{'a':'a'},{'a':'a'}] * 3
print(l2)
print(l2[0] is l2[3]) # True

"""
print([[0] * 3]) # [[0,0,0]]
# 相当于把数组中的每个元素复制三份
print([[0] * 3] * 3) # [[0,0,0], [0,0,0], [0,0,0]]

print([1 for _ in range(3)]) # [1, 1, 1]
 
# [[0] * cols for _ in range(rows)]








